package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.EduChapter;
import com.atguigu.edu.entity.EduVideo;
import com.atguigu.edu.entity.vo.EduChapterVo;
import com.atguigu.edu.entity.vo.EduLessonVo;
import com.atguigu.edu.mapper.EduChapterMapper;
import com.atguigu.edu.service.EduChapterService;
import com.atguigu.edu.service.EduVideoService;
import com.atguigu.service.base.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author alexzshl
 * @since 2021-05-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduVideoService eduVideoService;

    @Override
    public List<EduChapterVo> getChapterVideoByCourseId(String courseId) {
        ArrayList<EduChapterVo> chapterVoList = new ArrayList<>();

        // 根据课程id查询所有的章节 -> ChapterVo 列表
        List<EduChapter> eduChapterList = this.list(new QueryWrapper<EduChapter>()
                .eq("course_id", courseId).orderByAsc("sort"));
        // 根据课程id查询所有的小结 -> VideoVo 列表 (后面根据章节id过滤)
        List<EduVideo> eduVideoList = eduVideoService.list(new QueryWrapper<EduVideo>()
                .eq("course_id", courseId));

        eduChapterList.forEach(chapter -> {
            String chapterId = chapter.getId();
            EduChapterVo chapterVo = new EduChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);

            List<EduLessonVo> videoVoList = eduVideoList.stream()
                    .filter(eduVideo -> chapterId.equals(eduVideo.getChapterId()))
                    .sorted(Comparator.comparingInt(EduVideo::getSort))
                    .map(eduVideo -> {
                        EduLessonVo videoVo = new EduLessonVo();
                        BeanUtils.copyProperties(eduVideo, videoVo);
                        return videoVo;
                    })
                    .collect(Collectors.toList());
            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        });

        return chapterVoList;
    }

    @Override
    public Boolean addChapter(EduChapter chapter) {
        boolean save = this.save(chapter);
        return save;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        // 方式一: 删除章节的同时删除所拥有的小节

        // 方式二: 只允许删除空的章节(用户需要手动删除小结)
        // EduVideo existEduVideo = eduVideoService.getOne(new QueryWrapper<EduVideo>()
        //         .eq("chapter_id", chapterId));
        // if (existEduVideo != null) {
        //     throw new GuliException(20001, "只允许删除空的章节");
        // }
        int chapterVideoCount = eduVideoService.count(new QueryWrapper<EduVideo>().eq("chapter_id", chapterId));
        if (chapterVideoCount >= 1) {
            throw new GuliException(20001, "只允许删除空的章节");
        }

        // 删除章节
        boolean remove = this.removeById(chapterId);
        if(!remove) {
            throw new GuliException(20001, "删除章节失败");
        }
        return true;
    }

    @Override
    public Boolean updateChapter(EduChapter chapter) {
        boolean update = this.updateById(chapter);
        return update;
    }
}
